package example.runner2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

/**
 * GUI: von den Tanks (Sheepy) weitgehend übernommen.
 */
public class ControlField extends javax.swing.JFrame {
	private static final long serialVersionUID = -1389478513013808908L;
	private JPanel jSpielfeld;
	private Game _game;
	private static int SIZE = 600;

	/**
	 * Startet das Programm
	 */
	public static void main(String[] args) {
		new ControlField();
	}

	/**
	 * Erstellt das Fenster, zeigt es an und lässt das Spiel beginnen.
	 */
	public ControlField() {
		super();
		_game = new Game(this);
		initGUI();
		this.setVisible(true);
		_game.startGame();
	}

	private void initGUI() {
		try {
			this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setBounds(450, 0, SIZE + 20, SIZE + 50);
			{
				jSpielfeld = new PrivatePanel(_game);
				getContentPane().add(jSpielfeld, "Center");
				jSpielfeld.setBounds(10, 10, SIZE, SIZE);
				jSpielfeld.setLayout(null);
				jSpielfeld.setBorder(new LineBorder(
						new java.awt.Color(0, 0, 0), 1, false));
			}
			{
				JButton b = new JButton("Speed");
				b.setVisible(true);
				b.setBounds(jSpielfeld.getWidth() + 40, 30, 100, 40);
				b.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						setSpeed();
					}
				});
				getContentPane().add(b);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void setSpeed() {
		int result = Integer.parseInt(JOptionPane.showInputDialog(null));
		_game.setSpeedVerz(result);
		
	}

	private static class PrivatePanel extends JPanel {
		private static final long serialVersionUID = 2444492945961210136L;
		private Game _game;

		public PrivatePanel(Game game) {
			super();
			_game = game;
		}

		public void paint(Graphics g) {
			int[][] field = _game.getField();
			if (field != null) {
				this.setSize(SIZE, SIZE);
				for (int x = 0; x < Game.FIELD_WIDTH; x++) {
					for (int y = 0; y < Game.FIELD_HEIGHT; y++) {
						if (field[x][y] == 0) {
							g.setColor(new Color(200, 200, 200));
						} else if (field[x][y] == -1) {
							g.setColor(new Color(125, 125, 0));
						} else if (field[x][y] == -2) {
							g.setColor(new Color(0, 200, 200)); 
						} else if (field[x][y] == -3) {
							g.setColor(new Color(0, 0, 100));
						} else {
							double fitness = _game.getRunner(field[x][y] - 1)
									.getFitness();

							if (fitness >= 0.1) {
								if (fitness < 1) {
									g.setColor(new Color((int) (100+ 15.5 / fitness),
											0, 0));
								} else if (fitness >= 10) {
									g.setColor(new Color(10, 255, 0));
								} else {
									g.setColor(new Color((int) (255 / fitness),
											165,0));
								}
							}
							else
							{
								g.setColor(new Color(255, 0, 0));
							}

						}
						g.fillRect(x * SIZE / Game.FIELD_WIDTH, y * SIZE
								/ Game.FIELD_HEIGHT, SIZE / Game.FIELD_WIDTH,
								SIZE / Game.FIELD_HEIGHT);

					}
				}

			}

		}
	}

	public void close() {
		this.setVisible(false);
		jSpielfeld = null;
		this.dispose();
	}
}